/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-20
*/
package com.rzico.order.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.rzico.annotation.IgnoreSwaggerParameter;
import com.rzico.basics.entity.Supplier;
import com.rzico.core.entity.SysEmployee;
import com.rzico.core.entity.SysOffice;
import com.rzico.order.enumx.*;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import javax.persistence.Id;
import javax.persistence.Transient;

import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.data.annotation.*;

/**
 * <pre>
 *   采购单数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@Table(name = "wx_purchase")
@JsonIgnoreProperties(value = {"handler"})
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Purchase{
    @Id
    @ApiModelProperty(value = "" )
    protected Long id;

    @ApiModelProperty(value = "创建日期" , hidden = true)
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期" , hidden = true )
    protected java.util.Date modifyDate;

    @ApiModelProperty(value = "已付金额" )
    protected java.math.BigDecimal amountPaid;

    @ApiModelProperty(value = "应付金额" )
    protected java.math.BigDecimal amountPayable;

    @ApiModelProperty(value = "调整金额" )
    protected java.math.BigDecimal offsetAmount;

    @ApiModelProperty(value = "运费" )
    protected java.math.BigDecimal freight;

    @ApiModelProperty(value = "所在地" )
    protected Integer areaId;

    @ApiModelProperty(value = "地区名称" )
    protected String areaName;

    @ApiModelProperty(value = "收货地址" )
    protected String address;

    @ApiModelProperty(value = "收货人" )
    protected String consignee;

    @ApiModelProperty(value = "联系电话" )
    protected String phone;

    @JsonIgnore
    @ApiModelProperty(value = "是否删除" ,hidden = true )
    protected Boolean deleted;

    @ApiModelProperty(value = "业务日期", hidden = true )
    protected java.util.Date orderDate;

    @ApiModelProperty(value = "备注留言" )
    protected String memo;

    @ApiModelProperty(value = "订单状态(0.未确认,1.已确认,2.已完成,3.已取消)" )
    protected Integer orderStatus;

    @ApiModelProperty(value = "支付状态(0.未支付,1.已支付,2.待退款,3.已退款)" )
    protected Integer paymentStatus;

    @ApiModelProperty(value = "配送状态(0.未发货,1.已发货,2.待退货,3.已退货)" )
    protected Integer shippingStatus;

    @ApiModelProperty(value = "订单编号" )
    protected String sn;

    @ApiModelProperty(value = "商品合计金额" )
    protected java.math.BigDecimal subPrice;

    @ApiModelProperty(value = "商品合计数量" )
    protected BigDecimal subQuantity;

    @ApiModelProperty(value = "所属企业", hidden = true )
    protected Long enterpriseId;

    @ApiModelProperty(value = "供应商" )
    protected Long supplierId;

    @ApiModelProperty(value = "开票类型:0.不开票,1.增票,2.普票" )
    protected Integer taxType;

    @ApiModelProperty(value = "销项税率（%）", hidden = true )
    protected java.math.BigDecimal tax;

    @ApiModelProperty(value = "采购员", hidden = true )
    protected String operatorId;

    @ApiModelProperty(value = "所属部门" )
    protected String officeId;

    @JsonIgnore
    @ApiModelProperty(value = "创建者" ,hidden = true )
    protected String createBy;

    @JsonIgnore
    @ApiModelProperty(value = "更新者" ,hidden = true )
    protected String modifyBy;

    @ApiModelProperty(value = "订单明细")
    List<PurchaseItem> itemList = Collections.emptyList();

/*
    @JsonIgnore
    @IgnoreSwaggerParameter
    public SysEmployee sysEmployee;

    @ApiModelProperty(value = "人员名称", hidden = true)
    public String getOperatorName(){
        SysEmployee sysEmployee = getSysEmployee();
        if (null != sysEmployee){
            return sysEmployee.getName();
        }else{
            return "";
        }
    }

    public void setOperatorName(String operatorName){

    }


    @JsonIgnore
    @IgnoreSwaggerParameter
    public SysOffice sysOffice;

    @ApiModelProperty(value = "部门名称", hidden = true)
    public String getOfficeName(){
        SysOffice sysOffice = getSysOffice();
        if (null != sysOffice){
            return sysOffice.getName();
        }else{
            return "";
        }
    }

    public void setOfficeName(String officeName){

    }*/

    @JsonIgnore
    @IgnoreSwaggerParameter
    public Supplier supplier;

    @ApiModelProperty(value = "供应商名称", hidden = true)
    public String getSupplierName(){
        Supplier supplier = getSupplier();
        if (null != supplier){
            return supplier.getName();
        }else{
            return "";
        }
    }

    public void setSupplierName(String supplierName){

    }

    //非DB字段
    @JsonIgnore
    @Transient
    protected Long shopId;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateSubQuantity(){
        BigDecimal totalQuantity = new BigDecimal(0);
        if (null != itemList && itemList.size() > 0){
            for (PurchaseItem purchaseItem : itemList){
                totalQuantity =  totalQuantity.add(purchaseItem.getQuantity());
            }
        }
        return totalQuantity;
    }

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateSubPrice(){
        BigDecimal totalSubPrice = new BigDecimal(0);
        if (null != itemList && itemList.size() > 0){
            for (PurchaseItem purchaseItem : itemList){
                BigDecimal subPriceTotal = purchaseItem.getPrice().multiply(purchaseItem.getQuantity());
                totalSubPrice =  totalSubPrice.add(subPriceTotal);
            }
        }
        return totalSubPrice;
    }

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateDiscount(){
        BigDecimal totalSubPrice = new BigDecimal(0);
        if (null != itemList && itemList.size() > 0){
            for (PurchaseItem purchaseItem : itemList){
                totalSubPrice =  totalSubPrice.add(purchaseItem.getDiscount()==null?BigDecimal.ZERO:purchaseItem.getDiscount());
            }
        }
        return totalSubPrice;
    }


    //订单应收
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateAmountPayable (){
        BigDecimal totalAmountPayable = new BigDecimal(0);
        //加上运费，减去抹零
        totalAmountPayable = calculateSubPrice().subtract(calculateDiscount()).add(this.freight==null?BigDecimal.ZERO:this.freight).subtract(this.offsetAmount==null?BigDecimal.ZERO:this.offsetAmount);
        return totalAmountPayable;
    }



    @ApiModelProperty(value = "状态")
    public String getStatusDescr() {
        if (getOrderStatus().equals(PurchaseOrderStatusEnum.ORDER_STATUS_CANCLE.getId()) ) {
            return "已关闭";
        } else
        if (getOrderStatus().equals(PurchaseOrderStatusEnum.ORDER_STATUS_UNCONFIRM.getId())) {
            return "未确认";
        } else
        if (getShippingStatus().equals(PurchaseShippingStatusEnum.SHIPPING_STATUS_REFUND_SHIPPING.getId()) ) {
            return "已退货";
        } else
        if (getOrderStatus().equals(PurchaseOrderStatusEnum.ORDER_STATUS_CONFIRM.getId()) && getShippingStatus().equals(PurchaseShippingStatusEnum.SHIPPING_STATUS_UNSHIPPNG.getId()) ) {
            return "待入库";
        } else
        if (getOrderStatus().equals(PurchaseOrderStatusEnum.ORDER_STATUS_CONFIRM.getId()) && getShippingStatus().equals(PurchaseShippingStatusEnum.SHIPPING_STATUS_SHIPPNG.getId()) ) {
            return "已入库";
        } else
        if (getOrderStatus().equals(PurchaseOrderStatusEnum.ORDER_STATUS_COMPLETE.getId())) {
            return "已完成";
        } else {
            return "未知状态";
        }
    }

    public void setStatusDescr(String value) {

    }

}
